//
//  CPNewFeatrueCollectionViewController.swift
//  weibo
//
//  Created by zcp on 16/5/14.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private var itemCount = 4
class CPNewFeatrueCollectionViewController: UICollectionViewController {

    //布局
    let layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection  = UICollectionViewScrollDirection.Horizontal
        return layout
        }()
    override func viewDidLoad() {
        super.viewDidLoad()


        // Register cell classes
        self.collectionView!.registerClass(CPNewFeatrueCell.self, forCellWithReuseIdentifier: reuseIdentifier)
//        self.collectionView?.backgroundColor = UIColor.whiteColor()

        //设置分页,取消弹簧效果
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
            }
    init(){
    
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CPNewFeatrueCell
    
//        cell.backgroundColor = UIColor.yellowColor()
        cell.index = indexPath.item

    
        return cell
    }
    //MARK -- scorllView代理
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    
        //计算偏移量
        let index = Int((collectionView?.contentOffset.x)! / (collectionView?.bounds.width)!)
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! CPNewFeatrueCell
        if index == itemCount - 1{
            cell.addAnimotion()
        }
        
//        print("第\(index)页")
        
    }

   }
